﻿using Basic.UserRepository;
using WeDonekRpc.CacheClient.Interface;

namespace Basic.UserCollect.lmpl
{
    internal class ThirdAppCollect : IThirdAppCollect
    {
        private readonly IThirdAppRepository _Repository;
        private readonly ICacheController _Cache;

        public ThirdAppCollect (IThirdAppRepository repository, ICacheController cache)
        {
            this._Repository = repository;
            this._Cache = cache;
        }

        public int GetThirdId (string appId, string appType)
        {
            string key = string.Join("_", "ThirdApp", appId, appType);
            if (!this._Cache.TryGet(key, out int id))
            {
                id = this._Repository.GetThirdId(appId, appType);
                if (id == 0)
                {
                    _ = this._Cache.Add(key, id, new TimeSpan(0, 1, 0));
                }
                else
                {
                    _ = this._Cache.Add(key, id);
                }
            }
            return id;
        }
    }
}
